#include "Stdafx.h"
#include "CardBox.h"

CardBox::CardBox()
{
	srand( ( unsigned )time( NULL ) ) ;
	this->Create() ;
}

CardBox::CardBox( const CardBox& obj )
{
	this->element = new Array<Card>( *obj.element ) ; 
}

CardBox::~CardBox()
{
	this->Assert() ;
	delete this->element ; 
	this->element = NULL ;
}

void CardBox::Create()
{
	this->element = new Array<Card>(52);
	for( int i = 0 ; i < 52 ; ++i ) 
		this->element->At( i ).Set( i % 13 + 1 , i / 13 ) ;
}

void CardBox::Assert( void ) const
{
	assert( this->element != NULL ) ;
}

void CardBox::Assert( const int& index ) const
{
	assert( ( index >= 0 ) && ( this->Size() > index ) ) ;
}

const int CardBox::RandIndex( void )
{
	return rand() % this->Size() ;
}

Card& CardBox::operator [] ( const int& index ) const
{
	this->Assert( index ) ;
	return (*this->element)[index];
}

CardBox& CardBox::operator = ( const CardBox& obj ) 
{
	if( this != &obj )
		*this->element = *obj.element ;
	return *this ;
}

bool CardBox::Empty() const 
{
	this->Assert() ;
	return this->element->Empty() ;
}

int CardBox::Size() const
{
	this->Assert() ;
	return this->element->Size() ;
}

bool CardBox::Send( Card& obj )
{
	this->Assert() ;
	if ( this->Size() > 0 )
	{
		int index = this->RandIndex() ;
		obj = this->At( index ) ;
		this->Remove( index ) ;
		return true ;
	}
	return false ;
}

bool CardBox::Remove( const Card& obj )
{
	if ( this->Size() > 0 )
	{
		this->element->Clear( obj ) ;
	}
	return false ;

}

void CardBox::Remove( const int& index )
{
	this->Assert() ;
	this->Assert( index ) ;
	this->element->Remove( index ) ;
}

Card& CardBox::At( const int & index ) 
{
	return (*this)[index] ;
}

Card& CardBox::Rand( void )
{
	return (*this)[this->RandIndex()] ;
}

void CardBox::Reset()
{
	this->Assert() ;
	delete this->element ;
	this->Create() ;
}
